package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.PowerTreasure;
import com.ruoyi.system.mapper.PowerTreasureMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PowerOrderMapper;
import com.ruoyi.system.domain.PowerOrder;
import com.ruoyi.system.service.IPowerOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单表Service业务层处理
 * 
 * @author ljl
 * @date 2025-03-21
 */
@Service
public class PowerOrderServiceImpl implements IPowerOrderService 
{
    @Autowired
    private PowerOrderMapper powerOrderMapper;
    @Autowired
    private PowerTreasureMapper treasureMapper;

    /**
     * 查询订单表
     * 
     * @param orderId 订单表主键
     * @return 订单表
     */
    @Override
    public PowerOrder selectPowerOrderByOrderId(Long orderId)
    {
        return powerOrderMapper.selectPowerOrderByOrderId(orderId);
    }

    /**
     * 查询订单表列表
     * 
     * @param powerOrder 订单表
     * @return 订单表
     */
    @Override
    @DataScope(userAlias = "u")
    public List<PowerOrder> selectPowerOrderList(PowerOrder powerOrder)
    {
        return powerOrderMapper.selectPowerOrderList(powerOrder);
    }

    /**
     * 新增订单表
     * 
     * @param powerOrder 订单表
     * @return 结果
     */
    @Override
    public int insertPowerOrder(PowerOrder powerOrder)
    {
        powerOrder.setCreateTime(DateUtils.getNowDate());
        return powerOrderMapper.insertPowerOrder(powerOrder);
    }

    /**
     * 租借
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int rent(PowerOrder powerOrder) {
        // 新增订单
        powerOrder.setCreateTime(DateUtils.getNowDate());
        int rs = powerOrderMapper.insertPowerOrder(powerOrder);
        // 更新充电宝租借状态
        PowerTreasure powerTreasure = new PowerTreasure();
        powerTreasure.setTreasureId(powerOrder.getTreasureId());
        powerTreasure.setRentalStatus(0l);
        treasureMapper.updatePowerTreasure(powerTreasure);

        return rs;
    }

    /**
     * 修改订单表
     * 
     * @param powerOrder 订单表
     * @return 结果
     */
    @Override
    public int updatePowerOrder(PowerOrder powerOrder)
    {
        powerOrder.setUpdateTime(DateUtils.getNowDate());
        return powerOrderMapper.updatePowerOrder(powerOrder);
    }

    /**
     * 批量删除订单表
     * 
     * @param orderIds 需要删除的订单表主键
     * @return 结果
     */
    @Override
    public int deletePowerOrderByOrderIds(Long[] orderIds)
    {
        return powerOrderMapper.deletePowerOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单表信息
     * 
     * @param orderId 订单表主键
     * @return 结果
     */
    @Override
    public int deletePowerOrderByOrderId(Long orderId)
    {
        return powerOrderMapper.deletePowerOrderByOrderId(orderId);
    }

    @Override
    @Transactional
    public int endOrder(PowerOrder powerOrder, PowerTreasure powerTreasure) {

        int i = powerOrderMapper.updatePowerOrder(powerOrder);
        treasureMapper.updatePowerTreasure(powerTreasure);

        return i;
    }
}
